//
//  SchemeProgramViewController.swift
//  Runner
//
//  Created by 李旭峰 on 2020/10/10.
//

import UIKit
import WebKit

class SchemeProgramViewController: UIViewController {
    
    var url:URL?
    var isNeedGoBack:Bool = true
    var isNeedPopToRoot:Bool = false
    var isNeedRightBarItem:Bool = false
    let maskImageView = UIImageView(image: UIImage(named: "image_vrbg"))
    let contentView = UIView()
    lazy var webView:WKWebView = {
        let config = WKWebViewConfiguration()
        config.preferences.minimumFontSize = 9.0
        var webView = WKWebView(frame: CGRect.zero, configuration: config)
        return webView
    }()
    init(withUrl url:URL?) {
        self.url = url
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func loadView() {
        super.loadView()
        self.webView.frame = UIScreen.main.bounds
        self.maskImageView.frame = UIScreen.main.bounds
        self.maskImageView.contentMode = .scaleToFill
        self.maskImageView.layer.masksToBounds = true
        self.contentView.backgroundColor = UIColor.white
        self.contentView.frame = UIScreen.main.bounds
        self.view.addSubview(self.contentView)
        self.view.addSubview(self.maskImageView)
        self.contentView.addSubview(self.webView)
        if #available(iOS 11.0, *) {
            self.webView.scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.edgesForExtendedLayout = .all
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        if let tempUrl = self.url {
            self.resetUrlPath(tempUrl)
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear( animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    override var prefersStatusBarHidden: Bool {
        return true
    }
    func resetUrlPath(_ url:URL){
        DispatchQueue.main.async {
            self.url = url
            self.maskImageView.alpha = 1.0
            self.maskImageView.isHidden = false
            let config = WKWebViewConfiguration()
            config.preferences.minimumFontSize = 9.0
            let webView = WKWebView(frame: CGRect.zero, configuration: config)
            self.resetWebView(newWebView: webView)
            let request = URLRequest(url: self.url!)
            self.webView.load(request)
        }
    }
    func ClearCache() {
    }
    func resetWebView(newWebView:WKWebView){
        let oldWebView = self.webView
        oldWebView.uiDelegate = nil
        oldWebView.navigationDelegate = nil
        oldWebView.removeFromSuperview()
        
        self.webView = newWebView
        self.webView.frame = UIScreen.main.bounds
        self.contentView.addSubview(self.webView)
        if #available(iOS 11.0, *) {
            self.webView.scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.edgesForExtendedLayout = .all
        }
    }
    func initBridge(){
    }
    private func registerJSHandler(){
    }
    private func eveJsCode(params:[String:Any]){
    }
}
